
Partial Class admin_alertsSettings
    Inherits System.Web.UI.Page
    Private key As DataKey


    Private Sub SetupJavaScript()

        EntityController.JavaScriptAddChildDiv(Me.AlertsScrollingGridView.DivClientId, CInt(Me.AlertsScrollingGridView.ToolTip))
        ClientScript.RegisterStartupScript(Me.GetType(), "scrollDivs", EntityController.JavaScriptScrollingGridStartup(Me.AlertsScrollingGridView), True)
        EntityController.JavaScriptSetFixedHeight(0)
        ScriptManager.RegisterClientScriptBlock(Me.AlertsScrollingGridView, Me.AlertsScrollingGridView.GetType(), _
        "scrollDivAsync", EntityController.JavaScriptScrollingGridStartup(Me.AlertsScrollingGridView), True)
    End Sub

    Private Sub SetStatus(ByVal message As String)
        Me.AlertsScrollingGridView.StatusText = message
    End Sub

    Private Sub SetCriticalLevelStatus(ByVal message As String)
        Me.CriticalLevelsScrollingGridView.StatusText = message
    End Sub

    Private Sub ClearCriticalLevelStatus()
        SetCriticalLevelStatus("")
    End Sub

    Private Sub ClearStatus()
        SetStatus("")
    End Sub

    Protected Sub CriticalLevelsScrollingGridView_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles CriticalLevelsScrollingGridView.RowCancelingEdit
        SetCriticalLevelStatus("Alert Setting Update cancelled.")
    End Sub
    
    Protected Sub GridView2_RowEditing(ByVal sender As Object, _
                                       ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) _
                                       Handles CriticalLevelsScrollingGridView.RowEditing
        CriticalLevelsScrollingGridView.Focus()
    End Sub

    Protected Sub GridView2_RowUpdating(ByVal sender As Object, _
                                        ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) _
                                        Handles CriticalLevelsScrollingGridView.RowUpdating

        key = CriticalLevelsScrollingGridView.DataKeys(e.RowIndex)

        Dim objAlertSetting As Cdm.DataCore.Admin.AlertSettings
        objAlertSetting = Cdm.DataCore.Admin.AlertSettings.Retrieve(Convert.ToInt32(key.Value.ToString()))

        objAlertSetting.Number = Convert.ToInt32(e.NewValues.Item(0).ToString())
        objAlertSetting.Persist()
        e.Cancel = True
        Me.CriticalLevelsScrollingGridView.EditIndex = -1
        SetCriticalLevelStatus("Alert Settings saved.")

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ClearStatus()
        ClearCriticalLevelStatus()
        SetupJavaScript()
        If Not Page.IsPostBack Then
            Me.AlertsScrollingGridView.Sort("AlertSubject", SortDirection.Ascending)
        End If

    End Sub

    Protected Sub CancelButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CancelButton.Click

        Try

            Me.CriticalLevelsScrollingGridView.Visible = False
            Me.CancelButton.Visible = False

            Me.AlertsScrollingGridView.Visible = True
            SetCriticalLevelStatus("Edit cancelled.")

        Catch ex As Exception
            Cdm.AppServices.Log.ErrorLog("admin_alertsSettings", "An error occurred in the CancelButton_Click() method.", ex)
        End Try

    End Sub

    Protected Sub AlertsScrollingGridView_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles AlertsScrollingGridView.DataBound
        AccessibilityController.AddImageToSortableGridHeaders(AlertsScrollingGridView)
    End Sub

    Protected Sub AlertsScrollingGridView_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles AlertsScrollingGridView.RowCancelingEdit
        SetStatus("Update Cancelled.")
    End Sub

    Protected Sub AlertsScrollingGridView_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles AlertsScrollingGridView.RowDeleting
        e.Cancel = True
        key = Me.AlertsScrollingGridView.DataKeys(e.RowIndex)
        Me.AlertsScrollingGridView.Visible = False
        Me.AlertsScrollingGridView.Visible = False

        Me.AlertEditObjectDataSource.SelectParameters.Item(0).DefaultValue = key.Value.ToString()
        Me.AlertEditObjectDataSource.Select()

        Me.CriticalLevelsScrollingGridView.Caption = "Editing '" & Me.AlertsScrollingGridView.Rows(e.RowIndex).Cells(0).Text & "'"

        CriticalLevelsScrollingGridView.DataBind()
        CriticalLevelsScrollingGridView.Focus()
        CriticalLevelsScrollingGridView.Visible = True
        Me.CancelButton.Visible = True
    End Sub

    Protected Sub AlertsScrollingGridView_RowUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdatedEventArgs) Handles AlertsScrollingGridView.RowUpdated
        SetStatus("Alert text updated.")
    End Sub

    Protected Sub CriticalLevelsScrollingGridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles CriticalLevelsScrollingGridView.RowDataBound
        If e.Row.DataItemIndex = 0 Then
            AccessibilityController.AddImageToSortableGridHeaders(CriticalLevelsScrollingGridView)
        End If
        Dim txtValue As TextBox = CType(e.Row.FindControl("txtValue"), TextBox)
        If Not txtValue Is Nothing Then
            SetFocus(txtValue.ClientID)
            AccessibilityController.SetGridViewEditToolTips(e.Row, CriticalLevelsScrollingGridView)
        End If
    End Sub

    Protected Sub CriticalLevelsScrollingGridView_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles CriticalLevelsScrollingGridView.RowEditing
        CriticalLevelsScrollingGridView.Focus()
    End Sub

    Protected Sub CriticalLevelsScrollingGridView_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles CriticalLevelsScrollingGridView.RowUpdating
        key = CriticalLevelsScrollingGridView.DataKeys(e.RowIndex)

        Dim objAlertSetting As Cdm.DataCore.Admin.AlertSettings
        objAlertSetting = Cdm.DataCore.Admin.AlertSettings.Retrieve(Convert.ToInt32(key.Value.ToString()))

        objAlertSetting.Number = Convert.ToInt32(e.NewValues.Item(0).ToString())
        objAlertSetting.Persist()
        e.Cancel = True
        Me.CriticalLevelsScrollingGridView.EditIndex = -1
        SetCriticalLevelStatus("Alert Settings saved.")
    End Sub
End Class
